package cn.hyh.config;

import cn.hyh.component.MyInterceptor;
import cn.hyh.component.StringToDateConvert;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @auther :hyh
 * @desc :
 * @date :2020/1/6
 */
@Configuration                                                  //use-default-filters="false" 需要和 context:include-filter 一起使用.
@EnableWebMvc                                                  //不适用默认filters, 使用@controller的filters.
@ComponentScan(basePackages = "cn.hyh.controller",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(Controller.class)})
public class MvcChildConfig  implements WebMvcConfigurer {
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
	}

	@Override
	public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {

	}

	@Override
	public void addFormatters(FormatterRegistry registry) {
		  registry.addConverter(new StringToDateConvert());
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
         //这个设置在返回时使用。也算 produce的  mediaType
//		converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
	}

	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		for (HttpMessageConverter<?> converter : converters) {
			if (converter  instanceof StringHttpMessageConverter) {
				StringHttpMessageConverter c = (StringHttpMessageConverter) converter;
				  c.setDefaultCharset(Charset.forName("UTF-8"));
			}
		}
		converters.add(new MappingJackson2HttpMessageConverter());
	}

	//处理静态资源。  这种方式一般用于处理图片上传问题。
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//将资源的访问路径，映射到具体的静态资源路径。即当访问了 /resource/**静态资源，直接去对应的文件下查找。
      // registry.addResourceHandler("/resource").addResourceLocations("")
	}

	      //这种一般处理js 的问题
	@Override    //默认的处理器，当一个请求，没有被处理时，才被默认的处理器处理。
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		 configurer.enable();  //等价于开启一个默认的servelet
	}
}
